#pragma once
#include <wayland-server.h>
#include <functional>

class wl_listener_wrapper
{
public:
    using callback_t = std::function<void (void*)>;
    wl_listener_wrapper();
    ~wl_listener_wrapper();

    wl_listener_wrapper(const wl_listener_wrapper &) = delete;
    wl_listener_wrapper(wl_listener_wrapper &&) = delete;
    wl_listener_wrapper& operator =(const wl_listener_wrapper&) = delete;
    wl_listener_wrapper& operator =(wl_listener_wrapper&&) = delete;

#if 1
    /** Set the callback to be used when the signal is fired. Can be called
     * multiple times to update it */
    void set_callback(callback_t call);
    /** Connect this callback to a signal. Calling this on an already
     * connected listener will have no effect.
     * @return true if connection was successful */
    bool connect(wl_signal *signal);
#endif
    bool connect(wl_signal *signal, callback_t call);
    /** Disconnect from the wl_signal. No-op if not connected */
    void disconnect();
    /** @return true if connected to a wl_signal */
    bool is_connected() const;
    /** Call the stored callback. No-op if no callback was specified */
    void emit(void *data);

    struct wrapper
    {
        wl_listener listener;
        wl_listener_wrapper *self;
    };
private:
    callback_t call;
    wrapper _wrap;
};
